package com.jcrj.service;

import com.jcrj.dao.InfoManagerDao;
import com.jcrj.entity.OColumn;
import com.jcrj.entity.ODocument;
import com.jcrj.entity.OInfo;
import com.jcrj.entity.OInfoContent;
import com.jcrj.entity.OInfoOrg;
import com.jcrj.entity.OMenu;
import com.jcrj.entity.ONotice;
import com.jcrj.entity.ORole;
import com.jcrj.entity.OUser;
import java.util.Date;
import java.util.List;

public class InfoManagerService {
	private InfoManagerDao infoManagerDao;

	public InfoManagerDao getInfoManagerDao() {
		return this.infoManagerDao;
	}

	public void setInfoManagerDao(InfoManagerDao infoManagerDao) {
		this.infoManagerDao = infoManagerDao;
	}

	public void saveColumn(OColumn column) {
		this.infoManagerDao.saveColumn(column);
		OMenu menu = new OMenu();
		menu.setCreateTime(new Date());
		menu.setName(column.getName());
		menu.setPid(Integer.valueOf(157));
		menu.setPx(column.getId());
		menu.setUrl("infoListByColumn.action?columnId=" + column.getId() + "&columnName=" + column.getName());
		this.infoManagerDao.saveMenu(menu);
		column.setMenuId(menu.getId());
	}

	public List<OInfoOrg> findColumnOrg(int columnId) {
		String hql = "from OColumnOrg o where o.columnId=" + columnId;
		return this.infoManagerDao.findByHql(hql);
	}

	public void updateColumn(OColumn column) {
		this.infoManagerDao.updateColumn(column);
		OMenu menu = (OMenu) this.infoManagerDao.getObject(new OMenu(), column.getMenuId().intValue());
		menu.setName(column.getName());
		this.infoManagerDao.updateMenu(menu);
	}

	public void deleteColumns(String[] ids) {
		if ((ids != null) && (ids.length > 0)) {
			String columnIds = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(columnIds))
					columnIds = columnIds + ids[i];
				else {
					columnIds = columnIds + "," + ids[i];
				}
			}
			String hql = "delete from  OColumn o where o.id in(" + columnIds + ")";
			this.infoManagerDao.executeHql(hql);
			String hql3 = "delete OMenu o where o.px in(" + columnIds + ")";
			this.infoManagerDao.executeHql(hql3);
		}
	}

	public void saveInfo(OInfo info) {
		this.infoManagerDao.saveInfo(info);
	}

	public void saveInfo(OInfo info, String ids, String names, String content) {
		if ((ids == null) || ("".equals(ids))) {
			info.setAlt("1");
		}
		String recordId = Long.toString(new Date().getTime());
		OColumn o = (OColumn) this.infoManagerDao.getObject(new OColumn(), info.getColumnId().intValue());
		info.setColumnName(o.getName());
		info.setContentId(recordId);
		this.infoManagerDao.saveInfo(info);
		if ((ids != null) && (!"".equals(ids))) {
			String[] idArr = ids.split(",");
			String[] nameArr = names.split(",");
			for (int i = 0; i < idArr.length; i++) {
				OInfoOrg og = new OInfoOrg();
				og.setInfoId(info.getId());
				og.setOrgId(Integer.valueOf(Integer.parseInt(idArr[i])));
				og.setOrgName(nameArr[i]);
				this.infoManagerDao.saveColumnOrg(og);
			}
		}
		if ((content != null) && (!"".equals(content))) {
			OInfoContent in = new OInfoContent();
			in.setContent(content.getBytes());
			in.setFileSize(Integer.valueOf(content.getBytes().length));
			in.setId(recordId);
			this.infoManagerDao.saveUpdateInfoContent(in);
		}
	}

	public void saveInfoContent(String contentId,String content){
		if ((content != null) && (!"".equals(content))) {
			OInfoContent in = new OInfoContent();
			in.setContent(content.getBytes());
			in.setFileSize(Integer.valueOf(content.getBytes().length));
			in.setId(contentId);
			this.infoManagerDao.saveUpdateInfoContent(in);
		}
	}
	
	public OInfoContent getInfoContent(String recordId) {
		return (OInfoContent) this.infoManagerDao.getObject(new OInfoContent(), recordId);
	}

	public void updateInfo(OInfo info) {
		this.infoManagerDao.updateInfo(info);
	}

	public void publishInfos(String[] ids) {
		if ((ids != null) && (ids.length > 0)) {
			String infoIds = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(infoIds))
					infoIds = infoIds + ids[i];
				else {
					infoIds = infoIds + "," + ids[i];
				}
			}
			String hql = "update OInfo o set o.publish='1' where o.id in(" + infoIds + ")";
			this.infoManagerDao.executeHql(hql);
		}
	}

	public void deleteInfos(String[] ids) {
		if ((ids != null) && (ids.length > 0)) {
			String infoIds = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(infoIds))
					infoIds = infoIds + ids[i];
				else {
					infoIds = infoIds + "," + ids[i];
				}
			}
			String hql = "update OInfo o set o.del='1' where o.id in(" + infoIds + ")";
			this.infoManagerDao.executeHql(hql);
		}
	}

	public Object getObject(Object obj, int id) {
		return this.infoManagerDao.getObject(obj, id);
	}

	public List getOColumns() {
		String hql = "from OColumn ";
		return this.infoManagerDao.findByHql(hql);
	}

	public List getOColumnss(int page) {
		String hql = "from OColumn o  order by o.id desc";
		return this.infoManagerDao.findByPage(hql, page);
	}

	public long countTotalOColumns() {
		String hql = "select count(o.id) from OColumn o ";
		return this.infoManagerDao.countTotalByHql(hql).longValue();
	}

	public List<OInfo> getNotice(int page, String orgId, String beginTime, String endTime, String title) {
		String hql = "from OInfo where del='0'  and publish=1";
		if ((orgId == null) || ("".equals(orgId)))
			hql = hql + " and columnId in (1,2)";
		else {
			hql = hql + " and columnId =" + orgId;
		}
		if ((title != null) && (!"".equals(title))) {
			hql = hql + " and title like '%" + title.trim() + "%'";
		}
		if ((beginTime != null) && (!"".equals(beginTime))) {
			hql = hql + " and createTime >= to_date('" + beginTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endTime != null) && (!"".equals(endTime))) {
			hql = hql + " and createTime <= to_date('" + endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by id desc";
		return this.infoManagerDao.findByPage(hql, page);
	}

	public long countNotice(String orgId, String beginTime, String endTime, String title) {
		String hql = "select count(id) from OInfo where del='0'  and publish=1";

		if ((orgId == null) || ("".equals(orgId)))
			hql = hql + " and columnId in (1,2)";
		else {
			hql = hql + " and columnId =" + orgId;
		}
		if ((title != null) && (!"".equals(title))) {
			hql = hql + " and title like '%" + title.trim() + "%'";
		}
		if ((beginTime != null) && (!"".equals(beginTime))) {
			hql = hql + " and createTime >= to_date('" + beginTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endTime != null) && (!"".equals(endTime))) {
			hql = hql + " and createTime <= to_date('" + endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.infoManagerDao.countTotalByHql(hql).longValue();
	}

	public List getOInfos(String columnId, String title, String beginTime, String endTime, int page) {
		//===
		String hql = "from OInfo o where o.del='0' ";
		if ((columnId != null) && (!"".equals(columnId.trim())))
			hql = hql + " and o.columnId=" + columnId;
		else {
			hql = hql + " and columnId   in (1,2)";
		}
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.title like '%" + title.trim() + "%'";
		}
		if ((beginTime != null) && (!"".equals(beginTime.trim()))) {
			hql = hql + " and o.createTime >= to_date('" + beginTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endTime != null) && (!"".equals(endTime.trim()))) {
			hql = hql + " and o.createTime <= to_date('" + endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by o.id desc";
		return this.infoManagerDao.findByPage(hql, page);
	}

	public long countTotalOInfos(String columnId, String title, String beginTime, String endTime) {
		String hql = "select count(id) from OInfo o where o.del='0'  ";
		if ((columnId != null) && (!"".equals(columnId.trim())))
			hql = hql + " and o.columnId=" + columnId;
		else {
			hql = hql + " and columnId   in (1,2)";
		}
		if ((title != null) && (!"".equals(title.trim()))) {
			hql = hql + " and o.title like '%" + title.trim() + "%'";
		}
		if ((beginTime != null) && (!"".equals(beginTime.trim()))) {
			hql = hql + " and o.createTime >= to_date('" + beginTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endTime != null) && (!"".equals(endTime.trim()))) {
			hql = hql + " and o.createTime <= to_date('" + endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.infoManagerDao.countTotalByHql(hql).longValue();
	}

	public List<OInfo> findInfoByUser(int page, String columnId, String users, OUser user, String beginTime, String endTime, String title) {
		String hql = "from OInfo where del='0' and id in(select id from OInfo where  userId in(" + users
				+ ") or alt='1' or id in(select infoId from OInfoOrg where orgId =" + user.getOrgId() + ")) ";
		if ((columnId == null) || ("".equals(columnId)))
			hql = hql + " and columnId not in (1,2)";
		else {
			hql = hql + " and columnId =" + columnId;
		}
		if ((title != null) && (!"".equals(title))) {
			hql = hql + " and title like '%" + title.trim() + "%'";
		}
		if ((beginTime != null) && (!"".equals(beginTime))) {
			hql = hql + " and createTime >= to_date('" + beginTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endTime != null) && (!"".equals(endTime))) {
			hql = hql + " and createTime <= to_date('" + endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by id desc";
		return this.infoManagerDao.findByPage(hql, page);
	}

	public long countInfoByUser(String columnId, String users, OUser user, String beginTime, String endTime, String title) {
		String hql = "select count(id) from OInfo where del='0' and id in(select id from OInfo where  userId in (" + users
				+ ") or alt='1' or id in(select infoId from OInfoOrg where orgId =" + user.getOrgId() + ")) ";
		if ((columnId == null) || ("".equals(columnId)))
			hql = hql + " and columnId not in (1,2)";
		else {
			hql = hql + " and columnId =" + columnId;
		}
		if ((title != null) && (!"".equals(title))) {
			hql = hql + " and title like '%" + title.trim() + "%'";
		}
		if ((beginTime != null) && (!"".equals(beginTime))) {
			hql = hql + " and createTime >= to_date('" + beginTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endTime != null) && (!"".equals(endTime))) {
			hql = hql + " and createTime <= to_date('" + endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.infoManagerDao.countTotalByHql(hql).longValue();
	}

	public String getColumnName(int id) {
		OColumn column = (OColumn) this.infoManagerDao.getObject(new OColumn(), id);
		return column.getName();
	}

	public void saveNotice(int docId, String[] orgIds, OUser user) {
		ODocument doc = (ODocument) this.infoManagerDao.getObject(new ODocument(), docId);
		if ((orgIds != null) && (!"".equals(orgIds)))
			for (int i = 0; i < orgIds.length; i++) {
				ONotice notice = new ONotice();
				notice.setClick(Integer.valueOf(0));
				notice.setCreateTime(new Date());
				notice.setOrgId(Integer.valueOf(Integer.parseInt(orgIds[i])));
				notice.setContentId(doc.getContentId());
				notice.setTitle(doc.getTitle());
				notice.setUserId(user.getId());
				notice.setUserName(user.getName());
				this.infoManagerDao.saveNotice(notice);
			}
	}

	public String getUserAllOrgs(int orgId) {
		String sql = "select type,id from o_org start with id=" + orgId + "  connect by prior pid=id order by id desc";
		List<Object[]> list = this.infoManagerDao.findBySql(sql);
		String orgs = "";
		for (Object[] objs : list) {
			if (orgs.equals(""))
				orgs = objs[1].toString();
			else {
				orgs = orgs + "," + objs[1].toString();
			}
			if (objs[0].equals("1")) {
				break;
			}
		}
		return orgs;
	}

	public List<ONotice> findNoticeList(int page, String orgIds, String name, String beginTime, String endTime, String title, boolean role) {
		String hql = "from ONotice";
		if (!role)
			hql = hql + " where orgId in(" + orgIds + ")";
		else {
			hql = hql + " where 1=1";
		}
		if ((title != null) && (!"".equals(title))) {
			hql = hql + " and title like '%" + title.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and userName like '%" + name.trim() + "%'";
		}
		if ((beginTime != null) && (!"".equals(beginTime))) {
			hql = hql + " and createTime >= to_date('" + beginTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endTime != null) && (!"".equals(endTime))) {
			hql = hql + " and createTime <= to_date('" + endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		hql = hql + " order by createTime desc";
		return this.infoManagerDao.findByPage(hql, page);
	}

	public long countNoticeAll(String orgIds, String name, String beginTime, String endTime, String title, boolean role) {
		String hql = "select count(id) from ONotice ";
		if (!role)
			hql = hql + " where orgId in(" + orgIds + ")";
		else {
			hql = hql + " where 1=1";
		}
		if ((title != null) && (!"".equals(title))) {
			hql = hql + " and title like '%" + title.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name))) {
			hql = hql + " and userName like '%" + name.trim() + "%'";
		}
		if ((beginTime != null) && (!"".equals(beginTime))) {
			hql = hql + " and createTime >= to_date('" + beginTime + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if ((endTime != null) && (!"".equals(endTime))) {
			hql = hql + " and createTime <= to_date('" + endTime + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		return this.infoManagerDao.countTotalByHql(hql).longValue();
	}

	public boolean getRoleByUser(int userId) {
		String hql = "from ORole where id in (select roleId from ORoleUser where userId=" + userId + ")";
		List<ORole> roles = this.infoManagerDao.findByHql(hql);
		String roleId = "";
		for (ORole role : roles) {
			if ("4".equals(role.getType())) {
				return true;
			}
		}
		return false;
	}

	public void deleteNotice(String[] ids) {
		if ((ids != null) && (ids.length > 0)) {
			String infoIds = "";
			for (int i = 0; i < ids.length; i++) {
				if ("".equals(infoIds))
					infoIds = infoIds + ids[i];
				else {
					infoIds = infoIds + "," + ids[i];
				}
			}
			String hql = "delete from ONotice o where o.id in(" + infoIds + ")";
			this.infoManagerDao.executeHql(hql);
		}
	}
}